<?php
class LanguageController extends FrontController
{
	public function indexAction()
	{
		
	}
	
	public function loadAction()
	{
		$tbLang = new Table_Language();
		$rows = $tbLang->getList();
		return $rows;
	}
	
	public function loadTreeAction()
	{
		$items = $this->loadAction();
		$tree = array();
		foreach($items as &$item){
			$tree[] = array(
				'id' => $item['id'],
				'text' => $item['name'],
				'leaf' => true,
				'data' => $item
			);
		}
		
		return $tree;
	}
	
	public function addAction()
	{
		$tbLang = new Table_Language();
		$data = $this->getRequest()->getCollection('name', 'code');
		$default = $this->getRequest()->default;
		if('on' == $default){
			$data['default'] = 1;
			
			# update default = 0 for each record
			$row = $tbLang->update(array('default' => 0));
		}
		
		$id = $tbLang->insert($data);
		return array('id' => $id);
	}
	
	public function updateAction()
	{
		$tbLang = new Table_Language();
		
		$id = $this->getRequest()->id;
		$data = $this->getRequest()->getCollectionIf('name', 'code');
		
		$default = $this->getRequest()->default;
		if('on' == $default){
			$data['default'] = 1;
			
			# update default = 0 for each record
			$row = $tbLang->update(array('default' => 0));
		}
		
		$row = $tbLang->updateById($id, $data);
		return array('success' => is_numeric($row));
	}
	
	public function delAction()
	{
		$id = $this->getRequest()->id;
		$tbLang = new Table_Language();
		$row = $tbLang->deleteById($id);
		return array('success' => is_numeric($row));
	}
	
	public function reorderAction()
	{
		$id = $this->getRequest()->id;
		$index = $this->getRequest()->index;
		$oldIndex = $this->getRequest()->oldIndex;
		
		$update1 = new CF_Db_Update();
		$update2 = new CF_Db_Update();
		
		if($index > $oldIndex){
			$update1->table(
				'##_languages', 
				array('index' => new CF_Db_Expr('`index` - 1')), 
				new CF_Db_Expr('`index` > %s AND `index` <= %s', $oldIndex, $index)
			);
		}else{
			$update1->table(
				'##_languages', 
				array('index' => new CF_Db_Expr('`index` + 1')), 
				new CF_Db_Expr('`index` < %s AND `index` >= %s', $oldIndex, $index)
			);
		}
		
		$update2->table('##_languages', array('index' => $index), new CF_Db_Expr('id = %s', $id));
		
		CF_Db::getInstance()->query(array($update1, $update2));
	}
	
	public function loadCodeAction()
	{
		$tbLangCode = new Table_Language_Code();
		
		$limit = $this->getRequest()->limit;
		$offset = $this->getRequest()->start;
		
		$store = $tbLangCode->select('*', null, null, $limit, $offset);
		return array(
			'rows' => $store->getAll(),
			'total' => $store->getFoundRows()
		);
	}
	
	public function addCodeAction()
	{
		$tbLangCode = new Table_Language_Code();
		$data = $this->getRequest()->getCollection('name', 'description');
		
		$id = $tbLangCode->insert($data);
		return array('success' => is_numeric($id), 'id' => $id);
	}
	
	public function updateCodeAction()
	{
		$id = $this->getRequest()->id;
		$data = $this->getRequest()->getCollectionIf('name', 'description');
		
		$tbLangCode = new Table_Language_Code();
		$row = $tbLangCode->updateById($id, $data);
		
		return array('success' => is_numeric($row));
	}
	
	public function delCodeAction()
	{
		$ids = $this->getRequest()->ids;
		
		$tbLangCode = new Table_Language_Code();
		$rows = $tbLangCode->deleteById($ids);
		
		return array('success' => is_numeric($rows));
	}
	
	public function loadPhraseAction()
	{
		$request = $this->getRequest();
		
		$langId = $request->lang_id;
		$codeId = $request->code_id;
		$offset = $request->start;
		$count = $request->limit;
		
		
		$select = new CF_Db_Select();
		$select->table(array('p' => '##_language_phrases'), '*')
			->joinInner(array('l' => '##_languages'), 'l.id = p.lang_id', array('name'))
			->where('code_id = %s', $codeId)
			->limit($count, $offset);
			
		if(is_numeric($langId)){
			$select->where('lang_id = %s', $langId);
		}
			
		$store = $select->execute();
		return array(
			'total' => $store->getFoundRows(),
			'rows'	=> $store->getAll()
		);
	}
	
	public function updatePhraseAction()
	{
		$request = $this->getRequest();
		
		$langId = $request->lang_id;
		$codeId = $request->code_id;
		$data = $request->getCollectionIf('value');
		
		$tbPhrase = new Table_Language_Phrase();
		$rows = $tbPhrase->update(
			$data
			,new CF_Db_Expr('lang_id = %s AND code_id = %s', $langId, $codeId)
		);
		
		return array('success' => is_numeric($rows));
	}
}